//
//  RegisteredView.m
//  nonam
//
//  Created by liuchengming on 17/11/12.
//  Copyright © 2017年 liuchengming. All rights reserved.
//

#import "RegisteredView.h"

@interface RegisteredView ()
@property (strong, nonatomic) IBOutlet UITextField *uesrname;
@property (strong, nonatomic) IBOutlet UITextField *password;

@end

@implementation RegisteredView

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
}

- (IBAction)onRegistered:(id)sender
{
    //ViewController * con = [self.storyboard instantiateViewControllerWithIdentifier:@"no_1"];
    NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"http://xl18z.cn/node/reg?name=%@&password=%@&tel=123",_uesrname.text,_password.text]];
    NSURLRequest * req = [NSURLRequest requestWithURL:url];
    NSURLSession * session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask * task = [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
                                   {
                                       NSString * text = [[NSString alloc]initWithBytes:data.bytes length:data.length encoding:NSUTF8StringEncoding];
                                       NSLog(@"%@",text);
                                       NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
                                       //大中枢派发
                                       dispatch_async(dispatch_get_main_queue(), ^
                                                      {
                                                          if ([dict[@"errcode"]intValue] == 0)
                                                          {
                                                              NSString *str = [NSString stringWithFormat:@"注册成功！"];
                                                              UIAlertController *aler =[UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
                                                              [self presentViewController:aler animated:YES completion:^{
                                                                  
                                                              }];
                                                              UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action)
                                                              {
                                                              }];
                                                              [aler addAction:action];
                                                              
                                                          }else
                                                          {
                                                              NSString * str = [NSString stringWithFormat:@"注册成功！"];
                                                              UIAlertController * aler = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
                                                              [self presentViewController:aler animated:YES completion:^
                                                               {
                                                               }];
                                                              UIAlertAction * action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action)
                                                                                        {
                                                                                            
                                                                                        }];
                                                              [aler addAction:action];
                                                              NSLog(@"%@",dict[@"errinfo"]);
                                                          }
                                                      });
                                   }];
    [task resume];
}

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
